package com.appleyk.util;

import org.springframework.util.ResourceUtils;

import java.io.*;

/**
 * @author appleyk
 * @version v1.0.0
 * @date created on 2023/12/3:16:01
 * @description 文件工具类
 */
public class SnFileUtils {
    private SnFileUtils(){}
    public static File getFile(String path){
        File file ;
        try{
            if (path.contains("classpath")){
                file = ResourceUtils.getFile(path);
            }else{
                file = new File(path);
            }
        }catch (FileNotFoundException e){
            throw new RuntimeException(String.format("%s not found!",path));
        }
        if (!file.exists()){
            if(!file.exists()){
                throw new RuntimeException(String.format("%s not found!",path));
            }
        }
        return file;
    }

    /**
     * 获取文本文件的内容
     * @param file 文件
     * @param lineBreak 换行标识符
     * @return String 文本内容
     */
    public static String getFileContent(File file,String lineBreak) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String content = "";
        String line;
        while ((line = br.readLine()) != null) {
            /**文本的换行符暂定用"`"代替*/
            content += line + lineBreak;
        }
        /**关闭资源*/
        br.close();
        return content;
    }
}
